home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / kgdb.sun3 / values.c < prev   
Encoding:
C/C++ Source or Header  |  1989-08-11  |  26.2 KB  |  1,022 lines

  1. /* Low level packing and unpacking of values for GDB.
  2.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "symtab.h"
  24. #include "value.h"
  25.  
  26. /* The value-history records all the values printed
  27.    by print commands during this session.  Each chunk
  28.    records 60 consecutive values.  The first chunk on
  29.    the chain records the most recent values.
  30.    The total number of values is in value_history_count.  */
  31.  
  32. #define VALUE_HISTORY_CHUNK 60
  33.  
  34. struct value_history_chunk
  35. {
  36.   struct value_history_chunk *next;
  37.   value values[VALUE_HISTORY_CHUNK];
  38. };
  39.  
  40. /* Chain of chunks now in use.  */
  41.  
  42. static struct value_history_chunk *value_history_chain;
  43.  
  44. static int value_history_count;    /* Abs number of last entry stored */
  45.  
  46.  
  47. /* List of all value objects currently allocated
  48.    (except for those released by calls to release_value)
  49.    This is so they can be freed after each command.  */
  50.  
  51. static value all_values;
  52.  
  53. /* Allocate a  value  that has the correct length for type TYPE.  */
  54.  
  55. value
  56. allocate_value (type)
  57.      struct type *type;
  58. {
  59.   register value val;
  60.  
  61.   val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type));
  62.   VALUE_NEXT (val) = all_values;
  63.   all_values = val;
  64.   VALUE_TYPE (val) = type;
  65.   VALUE_LVAL (val) = not_lval;
  66.   VALUE_ADDRESS (val) = 0;
  67.   VALUE_FRAME (val) = 0;
  68.   VALUE_OFFSET (val) = 0;
  69.   VALUE_BITPOS (val) = 0;
  70.   VALUE_BITSIZE (val) = 0;
  71.   VALUE_REPEATED (val) = 0;
  72.   VALUE_REPETITIONS (val) = 0;
  73.   VALUE_REGNO (val) = -1;
  74.   return val;
  75. }
  76.  
  77. /* Allocate a  value  that has the correct length
  78.    for COUNT repetitions type TYPE.  */
  79.  
  80. value
  81. allocate_repeat_value (type, count)
  82.      struct type *type;
  83.      int count;
  84. {
  85.   register value val;
  86.  
  87.   val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type) * count);
  88.   VALUE_NEXT (val) = all_values;
  89.   all_values = val;
  90.   VALUE_TYPE (val) = type;
  91.   VALUE_LVAL (val) = not_lval;
  92.   VALUE_ADDRESS (val) = 0;
  93.   VALUE_FRAME (val) = 0;
  94.   VALUE_OFFSET (val) = 0;
  95.   VALUE_BITPOS (val) = 0;
  96.   VALUE_BITSIZE (val) = 0;
  97.   VALUE_REPEATED (val) = 1;
  98.   VALUE_REPETITIONS (val) = count;
  99.   VALUE_REGNO (val) = -1;
  100.   return val;
  101. }
  102.  
  103. /* Free all the values that have been allocated (except for those released).
  104.    Called after each command, successful or not.  */
  105.  
  106. void
  107. free_all_values ()
  108. {
  109.   register value val, next;
  110.  
  111.   for (val = all_values; val; val = next)
  112.     {
  113.       next = VALUE_NEXT (val);
  114.       free (val);
  115.     }
  116.  
  117.   all_values = 0;
  118. }
  119.  
  120. /* Remove VAL from the chain all_values
  121.    so it will not be freed automatically.  */
  122.  
  123. void
  124. release_value (val)
  125.      register value val;
  126. {
  127.   register value v;
  128.  
  129.   if (all_values == val)
  130.     {
  131.       all_values = val->next;
  132.       return;
  133.     }
  134.  
  135.   for (v = all_values; v; v = v->next)
  136.     {
  137.       if (v->next == val)
  138.     {
  139.       v->next = val->next;
  140.       break;
  141.     }
  142.     }
  143. }
  144.  
  145. /* Return a copy of the value ARG.
  146.    It contains the same contents, for same memory address,
  147.    but it's a different block of storage.  */
  148.  
  149. static value
  150. value_copy (arg)
  151.      value arg;
  152. {
  153.   register value val;
  154.   register struct type *type = VALUE_TYPE (arg);
  155.   if (VALUE_REPEATED (arg))
  156.     val = allocate_repeat_value (type, VALUE_REPETITIONS (arg));
  157.   else
  158.     val = allocate_value (type);
  159.   VALUE_LVAL (val) = VALUE_LVAL (arg);
  160.   VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
  161.   VALUE_OFFSET (val) = VALUE_OFFSET (arg);
  162.   VALUE_BITPOS (val) = VALUE_BITPOS (arg);
  163.   VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
  164.   VALUE_REGNO (val) = VALUE_REGNO (arg);
  165.   bcopy (VALUE_CONTENTS (arg), VALUE_CONTENTS (val),
  166.      TYPE_LENGTH (VALUE_TYPE (arg))
  167.      * (VALUE_REPEATED (arg) ? VALUE_REPETITIONS (arg) : 1));
  168.   return val;
  169. }
  170.  
  171. /* Access to the value history.  */
  172.  
  173. /* Record a new value in the value history.
  174.    Returns the absolute history index of the entry.  */
  175.  
  176. int
  177. record_latest_value (val)
  178.      value val;
  179. {
  180.   int i;
  181.   double foo;
  182.  
  183.   /* Check error now if about to store an invalid float.  We return -1
  184.      to the caller, but allow them to continue, e.g. to print it as "Nan". */
  185.   if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT) {
  186.     foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i);
  187.     if (i) return -1;        /* Indicate value not saved in history */
  188.   }
  189.  
  190.   /* Here we treat value_history_count as origin-zero
  191.      and applying to the value being stored now.  */
  192.  
  193.   i = value_history_count % VALUE_HISTORY_CHUNK;
  194.   if (i == 0)
  195.     {
  196.       register struct value_history_chunk *new
  197.     = (struct value_history_chunk *)
  198.       xmalloc (sizeof (struct value_history_chunk));
  199.       bzero (new->values, sizeof new->values);
  200.       new->next = value_history_chain;
  201.       value_history_chain = new;
  202.     }
  203.  
  204.   value_history_chain->values[i] = val;
  205.   release_value (val);
  206.  
  207.   /* Now we regard value_history_count as origin-one
  208.      and applying to the value just stored.  */
  209.  
  210.   return ++value_history_count;
  211. }
  212.  
  213. /* Return a copy of the value in the history with sequence number NUM.  */
  214.  
  215. value
  216. access_value_history (num)
  217.      int num;
  218. {
  219.   register struct value_history_chunk *chunk;
  220.   register int i;
  221.   register int absnum = num;
  222.  
  223.   if (absnum <= 0)
  224.     absnum += value_history_count;
  225.  
  226.   if (absnum <= 0)
  227.     {
  228.       if (num == 0)
  229.     error ("The history is empty.");
  230.       else if (num == 1)
  231.     error ("There is only one value in the history.");
  232.       else
  233.     error ("History does not go back to $$%d.", -num);
  234.     }
  235.   if (absnum > value_history_count)
  236.     error ("History has not yet reached $%d.", absnum);
  237.  
  238.   absnum--;
  239.  
  240.   /* Now absnum is always absolute and origin zero.  */
  241.  
  242.   chunk = value_history_chain;
  243.   for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
  244.        i > 0; i--)
  245.     chunk = chunk->next;
  246.  
  247.   return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
  248. }
  249.  
  250. /* Clear the value history entirely.
  251.    Must be done when new symbol tables are loaded,
  252.    because the type pointers become invalid.  */
  253.  
  254. void
  255. clear_value_history ()
  256. {
  257.   register struct value_history_chunk *next;
  258.   register int i;
  259.   register value val;
  260.  
  261.   while (value_history_chain)
  262.     {
  263.       for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
  264.     if (val = value_history_chain->values[i])
  265.       free (val);
  266.       next = value_history_chain->next;
  267.       free (value_history_chain);
  268.       value_history_chain = next;
  269.     }
  270.   value_history_count = 0;
  271. }
  272.  
  273. static void
  274. history_info (num_exp)
  275.      char *num_exp;
  276. {
  277.   register int i;
  278.   register value val;
  279.   register int num;
  280.  
  281.   if (num_exp)
  282.     num = parse_and_eval_address (num_exp) - 5;
  283.   else
  284.     num = value_history_count - 9;
  285.  
  286.   if (num <= 0)
  287.     num = 1;
  288.  
  289.   for (i = num; i < num + 10 && i <= value_history_count; i++)
  290.     {
  291.       val = access_value_history (i);
  292.       printf_filtered ("$%d = ", i);
  293.       value_print (val, stdout, 0, Val_pretty_default);
  294.       printf_filtered ("\n");
  295.     }
  296. }
  297.  
  298. /* Internal variables.  These are variables within the debugger
  299.    that hold values assigned by debugger commands.
  300.    The user refers to them with a '$' prefix
  301.    that does not appear in the variable names stored internally.  */
  302.  
  303. static struct internalvar *internalvars;
  304.  
  305. /* Look up an internal variable with name NAME.  NAME should not
  306.    normally include a dollar sign.
  307.  
  308.    If the specified internal variable does not exist,
  309.    one is created, with a void value.  */
  310.  
  311. struct internalvar *
  312. lookup_internalvar (name)
  313.      char *name;
  314. {
  315.   register struct internalvar *var;
  316.  
  317.   for (var = internalvars; var; var = var->next)
  318.     if (!strcmp (var->name, name))
  319.       return var;
  320.  
  321.   var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
  322.   var->name = concat (name, "", "");
  323.   var->value = allocate_value (builtin_type_void);
  324.   release_value (var->value);
  325.   var->next = internalvars;
  326.   internalvars = var;
  327.   return var;
  328. }
  329.  
  330. value
  331. value_of_internalvar (var)
  332.      struct internalvar *var;
  333. {
  334.   register value val;
  335.  
  336. #ifdef IS_TRAPPED_INTERNALVAR
  337.   if (IS_TRAPPED_INTERNALVAR (var->name))
  338.     return VALUE_OF_TRAPPED_INTERNALVAR (var);
  339. #endif 
  340.  
  341.   val = value_copy (var->value);
  342.   VALUE_LVAL (val) = lval_internalvar;
  343.   VALUE_INTERNALVAR (val) = var;
  344.   return val;
  345. }
  346.  
  347. void
  348. set_internalvar_component (var, offset, bitpos, bitsize, newval)
  349.      struct internalvar *var;
  350.      int offset, bitpos, bitsize;
  351.      value newval;
  352. {
  353.   register char *addr = VALUE_CONTENTS (var->value) + offset;
  354.  
  355. #ifdef IS_TRAPPED_INTERNALVAR
  356.   if (IS_TRAPPED_INTERNALVAR (var->name))
  357.     SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset);
  358. #endif
  359.  
  360.   if (bitsize)
  361.     modify_field (addr, (int) value_as_long (newval),
  362.           bitpos, bitsize);
  363.   else
  364.     bcopy (VALUE_CONTENTS (newval), addr,
  365.        TYPE_LENGTH (VALUE_TYPE (newval)));
  366. }
  367.  
  368. void
  369. set_internalvar (var, val)
  370.      struct internalvar *var;
  371.      value val;
  372. {
  373. #ifdef IS_TRAPPED_INTERNALVAR
  374.   if (IS_TRAPPED_INTERNALVAR (var->name))
  375.     SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0);
  376. #endif
  377.  
  378.   free (var->value);
  379.   var->value = value_copy (val);
  380.   release_value (var->value);
  381. }
  382.  
  383. char *
  384. internalvar_name (var)
  385.      struct internalvar *var;
  386. {
  387.   return var->name;
  388. }
  389.  
  390. /* Free all internalvars.  Done when new symtabs are loaded,
  391.    because that makes the values invalid.  */
  392.  
  393. void
  394. clear_internalvars ()
  395. {
  396.   register struct internalvar *var;
  397.  
  398.   while (internalvars)
  399.     {
  400.       var = internalvars;
  401.       internalvars = var->next;
  402.       free (var->name);
  403.       free (var->value);
  404.       free (var);
  405.     }
  406. }
  407.  
  408. static void
  409. convenience_info ()
  410. {
  411.   register struct internalvar *var;
  412.   int varseen = 0;
  413.  
  414.   for (var = internalvars; var; var = var->next)
  415.     {
  416. #ifdef IS_TRAPPED_INTERNALVAR
  417.       if (IS_TRAPPED_INTERNALVAR (var->name))
  418.     continue;
  419. #endif
  420.       if (!varseen)
  421.     {
  422.       printf ("Debugger convenience variables:\n\n");
  423.       varseen = 1;
  424.     }
  425.       printf ("$%s: ", var->name);
  426.       value_print (var->value, stdout, 0, Val_pretty_default);
  427.       printf ("\n");
  428.     }
  429.   if (!varseen)
  430.     printf ("No debugger convenience variables now defined.\n\
  431. Convenience variables have names starting with \"$\";\n\
  432. use \"set\" as in \"set $foo = 5\" to define them.\n");
  433. }
  434.  
  435. /* Extract a value as a C number (either long or double).
  436.    Knows how to convert fixed values to double, or
  437.    floating values to long.
  438.    Does not deallocate the value.  */
  439.  
  440. LONGEST
  441. value_as_long (val)
  442.      register value val;
  443. {
  444.   return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
  445. }
  446.  
  447. double
  448. value_as_double (val)
  449.      register value val;
  450. {
  451.   double foo;
  452.   int inv;
  453.   
  454.   foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
  455.   if (inv)
  456.     error ("Invalid floating value found in program.");
  457.   return foo;
  458. }
  459.  
  460. /* Unpack raw data (copied from debugee) at VALADDR
  461.    as a long, or as a double, assuming the raw data is described
  462.    by type TYPE.  Knows how to convert different sizes of values
  463.    and can convert between fixed and floating point.
  464.  
  465.    C++: It is assumed that the front-end has taken care of
  466.    all matters concerning pointers to members.  A pointer
  467.    to member which reaches here is considered to be equivalent
  468.    to an INT (or some size).  After all, it is only an offset.  */
  469.  
  470. /*
  471.  * To allow cross machine type debugging in Sprite we bcopy the 
  472.  * data before returning it.  This is done so we can debugger sun3
  473.  * from sun4 that have stricter alignment constraints.
  474.  */
  475.  
  476. LONGEST
  477. unpack_long (type, valaddr)
  478.      struct type *type;
  479.      char *valaddr;
  480. {
  481.   register enum type_code code = TYPE_CODE (type);
  482.   register int len = TYPE_LENGTH (type);
  483.   register int nosign = TYPE_UNSIGNED (type);
  484. #ifdef KGDB
  485.   union { 
  486.       double    foo; /* double forces alignment. */
  487.       char    buffer[8];
  488.   } tmpMem;
  489.   bcopy(valaddr,tmpMem.buffer,8);
  490.   valaddr = tmpMem.buffer;
  491. #endif
  492.  
  493.   if (code == TYPE_CODE_ENUM)
  494.     code = TYPE_CODE_INT;
  495.   if (code == TYPE_CODE_FLT)
  496.     {
  497.       if (len == sizeof (float))
  498.     return * (float *) valaddr;
  499.  
  500.       if (len == sizeof (double))
  501.     return * (double *) valaddr;
  502.     }
  503.   else if (code == TYPE_CODE_INT && nosign)
  504.     {
  505.       if (len == sizeof (char))
  506.     return * (unsigned char *) valaddr;
  507.  
  508.       if (len == sizeof (short))
  509.     return * (unsigned short *) valaddr;
  510.  
  511.       if (len == sizeof (int))
  512.     return * (unsigned int *) valaddr;
  513.  
  514.       if (len == sizeof (long))
  515.     return * (unsigned long *) valaddr;
  516. #ifdef LONG_LONG
  517.       if (len == sizeof (long long))
  518.     return * (unsigned long long *) valaddr;
  519. #endif
  520.     }
  521.   else if (code == TYPE_CODE_INT)
  522.     {
  523.       if (len == sizeof (char))
  524.     return * (char *) valaddr;
  525.  
  526.       if (len == sizeof (short))
  527.     return * (short *) valaddr;
  528.  
  529.       if (len == sizeof (int))
  530.     return * (int *) valaddr;
  531.  
  532.       if (len == sizeof (long))
  533.     return * (long *) valaddr;
  534.  
  535. #ifdef LONG_LONG
  536.       if (len == sizeof (long long))
  537.     return * (long long *) valaddr;
  538. #endif
  539.     }
  540.   else if (code == TYPE_CODE_PTR
  541.        || code == TYPE_CODE_REF)
  542.     {
  543.       if (len == sizeof (char *))
  544.     return (CORE_ADDR) * (char **) valaddr;
  545.     }
  546.   else if (code == TYPE_CODE_MEMBER)
  547.     error ("not implemented: member types in unpack_long");
  548.  
  549.   error ("Value not integer or pointer.");
  550. }
  551.  
  552. /* Return a double value from the specified type and address.
  553.    INVP points to an int which is set to 0 for valid value,
  554.    1 for invalid value (bad float format).  In either case,
  555.    the returned double is OK to use.  */
  556.  
  557. double
  558. unpack_double (type, valaddr, invp)
  559.      struct type *type;
  560.      char *valaddr;
  561.      int *invp;
  562. {
  563.   register enum type_code code = TYPE_CODE (type);
  564.   register int len = TYPE_LENGTH (type);
  565.   register int nosign = TYPE_UNSIGNED (type);
  566. #ifdef KGDB
  567.   union { 
  568.       double    foo; /* double forces alignment. */
  569.       char    buffer[8];
  570.   } tmpMem;
  571.   bcopy(valaddr,tmpMem.buffer,8);
  572.   valaddr = tmpMem.buffer;
  573. #endif
  574.  
  575.   *invp = 0;            /* Assume valid.   */
  576.   if (code == TYPE_CODE_FLT)
  577.     {
  578.       if (INVALID_FLOAT (valaddr, len))
  579.     {
  580.       *invp = 1;
  581.       return 1.234567891011121314;
  582.     }
  583.  
  584.       if (len == sizeof (float))
  585.     return * (float *) valaddr;
  586.  
  587.       if (len == sizeof (double))
  588.     {
  589.       /* Some machines require doubleword alignment for doubles.
  590.          This code works on them, and on other machines.  */
  591.       double temp;
  592.       bcopy ((char *) valaddr, (char *) &temp, sizeof (double));
  593.       return temp;
  594.     }
  595.     }
  596.   else if (code == TYPE_CODE_INT && nosign)
  597.     {
  598.       if (len == sizeof (char))
  599.     return * (unsigned char *) valaddr;
  600.  
  601.       if (len == sizeof (short))
  602.     return * (unsigned short *) valaddr;
  603.  
  604.       if (len == sizeof (int))
  605.     return * (unsigned int *) valaddr;
  606.  
  607.       if (len == sizeof (long))
  608.     return * (unsigned long *) valaddr;
  609.  
  610. #ifdef LONG_LONG
  611.       if (len == sizeof (long long))
  612.     return * (unsigned long long *) valaddr;
  613. #endif
  614.     }
  615.   else if (code == TYPE_CODE_INT)
  616.     {
  617.       if (len == sizeof (char))
  618.     return * (char *) valaddr;
  619.  
  620.       if (len == sizeof (short))
  621.     return * (short *) valaddr;
  622.  
  623.       if (len == sizeof (int))
  624.     return * (int *) valaddr;
  625.  
  626.       if (len == sizeof (long))
  627.     return * (long *) valaddr;
  628.  
  629. #ifdef LONG_LONG
  630.       if (len == sizeof (long long))
  631.     return * (long long *) valaddr;
  632. #endif
  633.     }
  634.  
  635.   error ("Value not floating number.");
  636.   /* NOTREACHED */
  637.   return (double) 0;        /* To silence compiler warning.  */
  638. }
  639.  
  640. /* Given a value ARG1 of a struct or union type,
  641.    extract and return the value of one of its fields.
  642.    FIELDNO says which field.
  643.  
  644.    For C++, must also be able to return values from static fields */
  645.  
  646. value
  647. value_field (arg1, fieldno)
  648.      register value arg1;
  649.      register int fieldno;
  650. {
  651.   register value v;
  652.   register struct type *type = TYPE_FIELD_TYPE (VALUE_TYPE (arg1), fieldno);
  653.   register int offset;
  654.  
  655.   /* Handle packed fields */
  656.  
  657.   offset = TYPE_FIELD_BITPOS (VALUE_TYPE (arg1), fieldno) / 8;
  658.   if (TYPE_FIELD_BITSIZE (VALUE_TYPE (arg1), fieldno))
  659.     {
  660.       v = value_from_long (type,
  661.                unpack_field_as_long (VALUE_TYPE (arg1),
  662.                          VALUE_CONTENTS (arg1),
  663.                          fieldno));
  664.       VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (VALUE_TYPE (arg1), fieldno) % 8;
  665.       VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (VALUE_TYPE (arg1), fieldno);
  666.     }
  667.   else
  668.     {
  669.       v = allocate_value (type);
  670.       bcopy (VALUE_CONTENTS (arg1) + offset,
  671.          VALUE_CONTENTS (v),
  672.          TYPE_LENGTH (type));
  673.     }
  674.   VALUE_LVAL (v) = VALUE_LVAL (arg1);
  675.   if (VALUE_LVAL (arg1) == lval_internalvar)
  676.     VALUE_LVAL (v) = lval_internalvar_component;
  677.   VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
  678.   VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1);
  679.   return v;
  680. }
  681.  
  682. value
  683. value_fn_field (arg1, fieldno, subfieldno)
  684.      register value arg1;
  685.      register int fieldno;
  686. {
  687.   register value v;
  688.   struct fn_field *f = TYPE_FN_FIELDLIST1 (VALUE_TYPE (arg1), fieldno);
  689.   register struct type *type = TYPE_FN_FIELD_TYPE (f, subfieldno);
  690.   struct symbol *sym;
  691.  
  692.   sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, subfieldno),
  693.                0, VAR_NAMESPACE, 0);
  694.   if (! sym) error ("Internal error: could not find physical method named %s",
  695.             TYPE_FN_FIELD_PHYSNAME (f, subfieldno));
  696.   
  697.   v = allocate_value (type);
  698.   VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
  699.   VALUE_TYPE (v) = type;
  700.   return v;
  701. }
  702.  
  703. /* Return a virtual function as a value.
  704.    ARG1 is the object which provides the virtual function
  705.    table pointer.
  706.    F is the list of member functions which contains the desired virtual
  707.    function.
  708.    J is an index into F which provides the desired virtual function.
  709.    TYPE is the basetype which first provides the virtual function table.  */
  710. value
  711. value_virtual_fn_field (arg1, f, j, type)
  712.      value arg1;
  713.      struct fn_field *f;
  714.      int j;
  715.      struct type *type;
  716. {
  717.   /* First, get the virtual function table pointer.  That comes
  718.      with a strange type, so cast it to type `pointer to long' (which
  719.      should serve just fine as a function type).  Then, index into
  720.      the table, and convert final value to appropriate function type.  */
  721.   value vfn, vtbl;
  722.   value vi = value_from_long (builtin_type_int, 
  723.                   (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
  724.   VALUE_TYPE (arg1) = TYPE_VPTR_BASETYPE (type);
  725.  
  726.   /* This type may have been defined before its virtual function table
  727.      was.  If so, fill in the virtual function table entry for the
  728.      type now.  */
  729.   if (TYPE_VPTR_FIELDNO (type) < 0)
  730.     TYPE_VPTR_FIELDNO (type)
  731.       = fill_in_vptr_fieldno (type);
  732.  
  733.   /* The virtual function table is now an array of structures
  734.      which have the form { int16 offset, delta; void *pfn; }.  */
  735.   vtbl = value_ind (value_field (arg1, TYPE_VPTR_FIELDNO (type)));
  736.   VALUE_TYPE (vtbl) = lookup_pointer_type (builtin_type_int);
  737.  
  738.   /* Index into the virtual function table.  */
  739.   vfn = value_struct_elt (value_subscript (vtbl, vi), 0, "pfn",
  740.               "internal error in virtual function lookup");
  741.  
  742.   /* Reinstantiate the function pointer with the correct type.  */
  743.   VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
  744.   return vfn;
  745. }
  746.  
  747. /* The value of a static class member does not depend
  748.    on its instance, only on its type.  If FIELDNO >= 0,
  749.    then fieldno is a valid field number and is used directly.
  750.    Otherwise, FIELDNAME is the name of the field we are
  751.    searching for.  If it is not a static field name, an
  752.    error is signaled.  TYPE is the type in which we look for the
  753.    static field member.  */
  754. value
  755. value_static_field (type, fieldname, fieldno)
  756.      register struct type *type;
  757.      char *fieldname;
  758.      register int fieldno;
  759. {
  760.   register value v;
  761.   struct symbol *sym;
  762.  
  763.   if (fieldno < 0)
  764.     {
  765.       register struct type *t = type;
  766.       /* Look for static field.  */
  767.       while (t)
  768.     {
  769.       int i;
  770.       for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
  771.         if (! strcmp (TYPE_FIELD_NAME (t, i), fieldname))
  772.           {
  773.         if (TYPE_FIELD_STATIC (t, i))
  774.           {
  775.             fieldno = i;
  776.             goto found;
  777.           }
  778.         else
  779.           error ("field `%s' is not static");
  780.           }
  781.       t = TYPE_BASECLASSES (t) ? TYPE_BASECLASS (t, 1) : 0;
  782.     }
  783.  
  784.       t = type;
  785.  
  786.       if (destructor_name_p (fieldname, t))
  787.     error ("use `info method' command to print out value of destructor");
  788.  
  789.       while (t)
  790.     {
  791.       int i, j;
  792.  
  793.       for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; i--)
  794.         {
  795.           if (! strcmp (TYPE_FN_FIELDLIST_NAME (t, i), fieldname))
  796.         {
  797.           error ("use `info method' command to print value of method \"%s\"", fieldname);
  798.         }
  799.         }
  800.       t = TYPE_BASECLASSES (t) ? TYPE_BASECLASS (t, 1) : 0;
  801.     }
  802.       error("there is no field named %s", fieldname);
  803.     }
  804.  
  805.  found:
  806.  
  807.   sym = lookup_symbol (TYPE_FIELD_STATIC_PHYSNAME (type, fieldno),
  808.                0, VAR_NAMESPACE, 0);
  809.   if (! sym) error ("Internal error: could not find physical static variable named %s", TYPE_FIELD_BITSIZE (type, fieldno));
  810.  
  811.   type = TYPE_FIELD_TYPE (type, fieldno);
  812.   v = value_at (type, (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
  813.   return v;
  814. }
  815.  
  816. long
  817. unpack_field_as_long (type, valaddr, fieldno)
  818.      struct type *type;
  819.      char *valaddr;
  820.      int fieldno;
  821. {
  822.   long val;
  823.   int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
  824.   int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
  825.  
  826.   bcopy (valaddr + bitpos / 8, &val, sizeof val);
  827.  
  828.   /* Extracting bits depends on endianness of the machine.  */
  829. #ifdef BITS_BIG_ENDIAN
  830.   val = val >> (sizeof val * 8 - bitpos % 8 - bitsize);
  831. #else
  832.   val = val >> (bitpos % 8);
  833. #endif
  834.  
  835.   val &= (1 << bitsize) - 1;
  836.   return val;
  837. }
  838.  
  839. void
  840. modify_field (addr, fieldval, bitpos, bitsize)
  841.      char *addr;
  842.      int fieldval;
  843.      int bitpos, bitsize;
  844. {
  845.   long oword;
  846.  
  847.   bcopy (addr, &oword, sizeof oword);
  848.  
  849.   /* Shifting for bit field depends on endianness of the machine.  */
  850. #ifdef BITS_BIG_ENDIAN
  851.   bitpos = sizeof (oword) * 8 - bitpos - bitsize;
  852. #endif
  853.  
  854.   oword &= ~(((1 << bitsize) - 1) << bitpos);
  855.   oword |= fieldval << bitpos;
  856.   bcopy (&oword, addr, sizeof oword);
  857. }
  858.  
  859. /* Convert C numbers into newly allocated values */
  860.  
  861. value
  862. value_from_long (type, num)
  863.      struct type *type;
  864.      register LONGEST num;
  865. {
  866.   register value val = allocate_value (type);
  867.   register enum type_code code = TYPE_CODE (type);
  868.   register int len = TYPE_LENGTH (type);
  869.  
  870.   if (code == TYPE_CODE_INT || code == TYPE_CODE_ENUM)
  871.     {
  872.       if (len == sizeof (char))
  873.     * (char *) VALUE_CONTENTS (val) = num;
  874.       else if (len == sizeof (short))
  875.     * (short *) VALUE_CONTENTS (val) = num;
  876.       else if (len == sizeof (int))
  877.     * (int *) VALUE_CONTENTS (val) = num;
  878.       else if (len == sizeof (long))
  879.     * (long *) VALUE_CONTENTS (val) = num;
  880. #ifdef LONG_LONG
  881.       else if (len == sizeof (long long))
  882.     * (long long *) VALUE_CONTENTS (val) = num;
  883. #endif
  884.       else
  885.     error ("Integer type encountered with unexpected data length.");
  886.     }
  887.   else
  888.     error ("Unexpected type encountered for integer constant.");
  889.  
  890.   return val;
  891. }
  892.  
  893. value
  894. value_from_double (type, num)
  895.      struct type *type;
  896.      double num;
  897. {
  898.   register value val = allocate_value (type);
  899.   register enum type_code code = TYPE_CODE (type);
  900.   register int len = TYPE_LENGTH (type);
  901.  
  902.   if (code == TYPE_CODE_FLT)
  903.     {
  904.       if (len == sizeof (float))
  905.     * (float *) VALUE_CONTENTS (val) = num;
  906.       else if (len == sizeof (double))
  907.     * (double *) VALUE_CONTENTS (val) = num;
  908.       else
  909.     error ("Floating type encountered with unexpected data length.");
  910.     }
  911.   else
  912.     error ("Unexpected type encountered for floating constant.");
  913.  
  914.   return val;
  915. }
  916.  
  917. /* Deal with the value that is "about to be returned".  */
  918.  
  919. /* Return the value that a function returning now
  920.    would be returning to its caller, assuming its type is VALTYPE.
  921.    RETBUF is where we look for what ought to be the contents
  922.    of the registers (in raw form).  This is because it is often
  923.    desirable to restore old values to those registers
  924.    after saving the contents of interest, and then call
  925.    this function using the saved values.
  926.    struct_return is non-zero when the function in question is
  927.    using the structure return conventions on the machine in question;
  928.    0 when it is using the value returning conventions (this often
  929.    means returning pointer to where structure is vs. returning value). */
  930.  
  931. value
  932. value_being_returned (valtype, retbuf, struct_return)
  933.      register struct type *valtype;
  934.      char retbuf[REGISTER_BYTES];
  935.      int struct_return;
  936. {
  937.   register value val;
  938.  
  939.   if (struct_return)
  940.     return value_at (valtype, EXTRACT_STRUCT_VALUE_ADDRESS (retbuf));
  941.  
  942.   val = allocate_value (valtype);
  943.   EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS (val));
  944.  
  945.   return val;
  946. }
  947.  
  948. /* Return true if the function specified is using the structure returning
  949.    convention on this machine to return arguments, or 0 if it is using
  950.    the value returning convention.  FUNCTION is the value representing
  951.    the function, FUNCADDR is the address of the function, and VALUE_TYPE
  952.    is the type returned by the function */
  953.  
  954. struct block *block_for_pc ();
  955.  
  956. int
  957. using_struct_return (function, funcaddr, value_type)
  958.      value function;
  959.      CORE_ADDR funcaddr;
  960.      struct type *value_type;
  961. {
  962.   register enum type_code code = TYPE_CODE (value_type);
  963.  
  964.   if (code == TYPE_CODE_STRUCT ||
  965.       code == TYPE_CODE_UNION ||
  966.       code == TYPE_CODE_ARRAY)
  967.     {
  968.       struct block *b = block_for_pc (funcaddr);
  969.  
  970.       if (!(BLOCK_GCC_COMPILED (b) && TYPE_LENGTH (value_type) < 8))
  971.     return 1;
  972.     }
  973.  
  974.   return 0;
  975. }
  976.  
  977. /* Store VAL so it will be returned if a function returns now.
  978.    Does not verify that VAL's type matches what the current
  979.    function wants to return.  */
  980.  
  981. void
  982. set_return_value (val)
  983.      value val;
  984. {
  985.   register enum type_code code = TYPE_CODE (VALUE_TYPE (val));
  986.   char regbuf[REGISTER_BYTES];
  987.   double dbuf;
  988.   LONGEST lbuf;
  989.  
  990.   if (code == TYPE_CODE_STRUCT
  991.       || code == TYPE_CODE_UNION)
  992.     error ("Specifying a struct or union return value is not supported.");
  993.  
  994.   if (code == TYPE_CODE_FLT)
  995.     {
  996.       dbuf = value_as_double (val);
  997.  
  998.       STORE_RETURN_VALUE (VALUE_TYPE (val), &dbuf);
  999.     }
  1000.   else
  1001.     {
  1002.       lbuf = value_as_long (val);
  1003.       STORE_RETURN_VALUE (VALUE_TYPE (val), &lbuf);
  1004.     }
  1005. }
  1006.  
  1007. void
  1008. _initialize_values ()
  1009. {
  1010.   add_info ("convenience", convenience_info,
  1011.         "Debugger convenience (\"$foo\") variables.\n\
  1012. These variables are created when you assign them values;\n\
  1013. thus, \"print $foo=1\" gives \"$foo\" the value 1.  Values may be any type.\n\n\
  1014. A few convenience variables are given values automatically GDB:\n\
  1015. \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
  1016. \"$__\" holds the contents of the last address examined with \"x\".");
  1017.  
  1018.   add_info ("history", history_info,
  1019.         "Elements of value history (around item number IDX, or last ten).");
  1020. }
  1021.  
  1022.